<template>
    <div class="warehouse-index yuan-container">
        <a-card title="仓库管理">
            <div class="filter-panel">
                <a-input-search class="filter-list" allowClear style="width: 200px;color: #05b089" v-model="keywords"
                    placeholder="请输入关键词" @search="resetList" />
                <div class="filter-list" style="float: right;" v-if="$utilCheckPermission(['storeManagementAdd'])">
                    <a-button type="primary" @click="add">新增</a-button>
                </div>
            </div>
            <div class="table">
                <a-table bordered :rowKey="getRowKey" :data-source="pageList" :columns="columns" :pagination="false"
                    :loading="loading">
                </a-table>
            </div>
        </a-card>
        <Add :visible="addVisible" @closeModal="() => addVisible = false" :infoDetail="itemDetail" @resetList="getList" />
        <Detail :visible="detailVisible" @closeModal="() => detailVisible = false" :infoDetail="itemDetail" />

    </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { h } from "vue";
import nextApi, { WarehouseListRes } from "@/api/nextApi";
import Add from "./components/Add.vue";
import Detail from "./components/Detail.vue";
@Component({
    name: "warehouseIndex",
    components: { Add, Detail },
})
export default class extends Vue {
    previewVisible = false;
    keywords = "";
    loading = false;
    pagination = {
        pageSize: 10,
        current: 1,
        total: 0,
        showTotal: (total: number) => `共${total}条`,
        onChange: this.pageChange,
    };
    pageList: WarehouseListRes[] = [];
    columns = [
        {
            title: "序号",
            dataIndex: "index",
            key: "index",
            align: "center",
            customRender: (text: string, record: WarehouseListRes, index: number) => `${index + 1}`,
        },
        {
            align: "center",
            dataIndex: "name",
            key: "name",
            title: "仓库名称",
        },
        {
            align: "center",
            dataIndex: "address",
            key: "address",
            title: "仓库地址",
        },
        {
            align: "center",
            dataIndex: "state",
            key: "state",
            title: "状态",
            customRender: (text: string, record: WarehouseListRes, index: number) => {
                const stateBox = h(
                    "div",
                    { class: record.state === 1 ? "c-success" : "c-danger" },
                    record.state === 1 ? "启用" : "禁用"
                );
                return stateBox;
            },
        },
        {
            align: "center",
            dataIndex: "action",
            key: "action",
            title: "操作",
            width: 260,
            customRender: (text: string, record: WarehouseListRes, index: number) => {
                const statusDom = h(
                    "a-popconfirm",
                    {
                        props: {
                            title: record.state === 1 ? "确定要禁用吗？" : "确定要启用吗？",
                            okText: "确定",
                            cancelText: "取消",
                        },
                        on: { confirm: () => this.clickDisable(record) },
                    },
                    [
                        h(
                            "a-button",
                            {
                                style: { marginRight: "5px" },
                                props: {
                                    icon: record.state === 1 ? "delete" : "tool",
                                    type: record.state === 1 ? "danger" : "primary",
                                    size: "small",
                                },
                            },
                            record.state === 1 ? "禁用" : "启用"
                        ),
                    ]
                );
                const detailBtn = h(
                    "a-button",
                    {
                        style: { marginRight: "5px" },
                        props: { icon: "eye", type: "primary", size: "small" },
                        on: { click: () => this.showDetail(record) },
                    },
                    "详情"
                );
                const editBtn = h(
                    "a-button",
                    {
                        style: { marginRight: "5px" },
                        props: { icon: "edit", type: "primary", size: "small" },
                        on: { click: () => this.editDetail(record) },
                    },
                    "编辑"
                );

                const btn: any[] = [];
                if (this.$utilCheckPermission(["storeManagementDetails"])) btn.push(detailBtn);
                if (this.$utilCheckPermission(["storeManagementForbidden"])) btn.push(statusDom);
                if (this.$utilCheckPermission(["storeManagementCompile"])) btn.push(editBtn);
                return h("div", btn);
            },
        },
    ];

    getRowKey(item: any) {
        return item.id;
    }

    async getList() {
        this.pageList = [];
        try {
            this.loading = true;
            const res = await nextApi.getWarehouseList({
                name: this.keywords,
            });
            this.pageList = res.data;
        } finally {
            this.loading = false;
        }
    }
    resetList() {
        this.pagination.current = 1;
        this.getList();
    }
    itemDetail = {} as WarehouseListRes;
    detailVisible = false;
    showDetail(item: WarehouseListRes) {
        this.itemDetail = item;
        this.detailVisible = true;
    }

    addVisible = false;
    editDetail(item: WarehouseListRes) {
        this.itemDetail = item;
        this.addVisible = true;
    }
    add() {
        this.itemDetail = {} as WarehouseListRes;
        this.addVisible = true;
    }
    pageChange(page: number, pageSize: number) {
        this.pagination.pageSize = pageSize;
        this.pagination.current = page;

        this.getList();
    }
    async clickDisable(item: WarehouseListRes) {
        try {
            this.loading = true;
            await nextApi.setWarehouseState({
                ...item,
                state: item.state === 1 ? 2 : 1,
            });
        } finally {
            this.loading = false;
        }
        this.getList();
    }
    async mounted() {
        this.getList();
    }
    showStandardImg(item: any) {
        this.previewVisible = true;
    }
}
</script>
<style lang="scss">
.warehouse-index {
    padding: 20px;
}
</style>
